//
//  ImageMainMenuBarView.m
//  ImageEnginePlugin
//
//  Created by VNG on 10/18/12.
//
//

#import "ImageMainMenuBarView.h"
#include "Utils.h"

@implementation ImageMainMenuBarView

- (id)initWithNumButton:(NSInteger )_numButton
              arrayName:(NSArray* )_arrayName
       arrayImageNormal:(NSArray *)_arrayImageNormal
        arrayImageTouch:(NSArray *)_arrayImageTouch
             sizeButton:(CGSize)_sizeButton
            spaceButton:(NSInteger)_spaceButton
              infoFrame:(CGRect)_otherframe
        imageBackground:(UIImage *)_image
               delegate:(NSObject <ImageMenuBarViewDelegate>*)_otherDelegate
           hasEffectOut:(BOOL)_hasOut
{
    if ((self = [super init]))
    {
        self.frame = _otherframe;
        
        if (_image != NULL) {
            [self setBackgroundColor:[UIColor colorWithPatternImage:_image]];
        }else{
            [self setBackgroundColor:[UIColor blackColor]];
        }
        
        [self setCanCancelContentTouches:NO];
        self.showsHorizontalScrollIndicator = FALSE;
        self.showsVerticalScrollIndicator = FALSE;
        self.clipsToBounds = YES;
        self.scrollEnabled = YES;
        self.zoomScale = FALSE;
        
        menuBarDelegate = _otherDelegate;
        hasEffectOut = _hasOut;
        numButton = _numButton;
        sizeButton = _sizeButton;
        spaceButton = _spaceButton;
        
        CGSize sizeContent = CGSizeMake(sizeButton.width * numButton + (numButton - 1)*spaceButton + spaceButton*2, sizeButton.height);
        
        [self setContentSize:sizeContent];
        
        CGFloat curXButton = spaceButton;
        CGFloat YButton = (_otherframe.size.height - sizeButton.height)/2;
        for (int i = 0; i < numButton; i++)
        {
            UIButton *button = [[UIButton  alloc] init];
            
            CGRect frameButton = CGRectMake(curXButton, YButton, sizeButton.width, sizeButton.height);
            button.frame = frameButton;
                        
            if (_arrayName != NULL) {
                NSString *buttonName = (NSString* )[_arrayName objectAtIndex:i];
                UILabel* indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, sizeButton.height - 12, sizeButton.width, 12)];
                indexLabel.backgroundColor      = RGBACOLOR(0, 0, 0, 0.7);
                indexLabel.text                 = buttonName;
                indexLabel.font                 = [UIFont systemFontOfSize:10];
                indexLabel.textColor            = [UIColor whiteColor];
                indexLabel.textAlignment        = UITextAlignmentCenter;
                indexLabel.baselineAdjustment   = UIBaselineAdjustmentAlignCenters;
                [button addSubview:indexLabel];
                [indexLabel release];
//                [button setTitle:buttonName forState:UIControlStateNormal];
//                [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
//                [button setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
            }
            
            if (_arrayImageNormal != NULL) {
                UIImage* imageButtonNormal = (UIImage * )[_arrayImageNormal objectAtIndex:i];
                if (imageButtonNormal != nil) {
                    [button setBackgroundImage:imageButtonNormal forState:UIControlStateNormal];
                }
            }
            
            if (_arrayImageTouch != NULL) {
                UIImage* imageButtonTouch = (UIImage * )[_arrayImageTouch objectAtIndex:i];
                if (imageButtonTouch != nil) {
                    [button setBackgroundImage:imageButtonTouch forState:UIControlStateSelected];
                }
            }
            
            if (!hasEffectOut) {
                [[button layer] setCornerRadius:4.0];
                [[button layer] setMasksToBounds:YES];
                [[button layer] setBorderWidth:1.0];
                [[button layer] setBorderColor:UIColorFromRGB(0x666666).CGColor];
            }
            
            button.tag = i;
            [button addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
            
            if (i == 0) {
                [[button layer] setBorderWidth:2.0];
                [[button layer] setBorderColor:UIColorFromRGB(0x21BAF0).CGColor];
            }
            
            [self addSubview:button];
            
            [button release];
            
            curXButton += sizeButton.width + spaceButton;
        }
        
        //kindMenuBar = kMenuBarMain;
        kindMenuBar = 0;
    }
    
    indexSelectIndex = 0;
    
    return self;
}

- (id)initWithNumButton:(NSInteger )_numButton
              arrayName:(NSArray* )_arrayName
              tintColor:(NSArray*)_arrayColorButton
             sizeButton:(CGSize)_sizeButton
            spaceButton:(NSInteger)_spaceButton
              infoFrame:(CGRect)_otherframe
        imageBackground:(UIImage *)_image
               delegate:(NSObject <ImageMenuBarViewDelegate>*)_otherDelegate
           hasEffectOut:(BOOL)_hasOut
{
    if ((self = [super init])) {
        self.frame = _otherframe;
        
        if (_image != NULL) {
            [self setBackgroundColor:[UIColor colorWithPatternImage:_image]];
        }else{
            [self setBackgroundColor:[UIColor blackColor]];
        }
        
        [self setCanCancelContentTouches:NO];
        self.showsHorizontalScrollIndicator = FALSE;
        self.showsVerticalScrollIndicator = FALSE;
        self.clipsToBounds = YES;
        self.scrollEnabled = YES;
        self.zoomScale = FALSE;
        
        menuBarDelegate = _otherDelegate;
        hasEffectOut = _hasOut;
        numButton = _numButton;
        sizeButton = _sizeButton;
        spaceButton = _spaceButton;
        
        CGSize sizeContent = CGSizeMake(sizeButton.width * numButton + (numButton - 1)*spaceButton + spaceButton*2, sizeButton.height);
        
        [self setContentSize:sizeContent];
        
        CGFloat curXButton = spaceButton;
        CGFloat YButton = (_otherframe.size.height - sizeButton.height)/2;
        for (int i = 0; i < numButton; i++)
        {
            UIButton *button = [[UIButton  alloc] init];
            
            CGRect frameButton = CGRectMake(curXButton, YButton, sizeButton.width, sizeButton.height);
            button.frame = frameButton;
            
            if (_arrayName != NULL) {
                NSString *buttonName = (NSString* )[_arrayName objectAtIndex:i];
                [button setTitle:buttonName forState:UIControlStateNormal];
                [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
                [button setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
            }
            
            if (_arrayColorButton != NULL) {
                UIColor* colorButton = (UIColor*)[_arrayColorButton objectAtIndex:i];
                [button setBackgroundColor:colorButton];
            }
            
            if (!hasEffectOut) {
                [[button layer] setCornerRadius:4.0];
                [[button layer] setMasksToBounds:YES];
                [[button layer] setBorderWidth:1.0];
                [[button layer] setBorderColor:UIColorFromRGB(0x666666).CGColor];
            }
            
            button.tag = i;
            [button addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
            
            if (i == 0) {
                [[button layer] setBorderWidth:2.0];
                [[button layer] setBorderColor:UIColorFromRGB(0x21BAF0).CGColor];
            }
            
            [self addSubview:button];
            [button release];
            
            curXButton += sizeButton.width + spaceButton;
        }
        
        //kindMenuBar = kMenuBarMain;
        kindMenuBar = 0;
    }
    
    indexSelectIndex = 0;
    
    return self;
}

@end
